/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.command;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.ConnectorEnd;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.StructuredClassifier;

import com.cea.papyrus.core.utils.Debug;

/**
 * 
 * 
 * @author Patrick Tessier
 * @since 31 ao�t 06
 * @deprecated
 */
public class DeleteConnectorFromModelCommand extends Command {
	
	/**
	 * Dependency to delete.
	 */
	private org.eclipse.uml2.uml.Connector child;
	
	/**
	 * Owner of the dependency to delete.
	 */
	private Element parent;
	
	/**
	 * Save source and target element.
	 */
	private List<ConnectorEnd> memberEnds = new ArrayList<ConnectorEnd>();
	
	/**
	 * Default Constructor.
	 * 
	 * @param child 
	 */
	public DeleteConnectorFromModelCommand(Connector child) {
		if (child == null) {
			throw new IllegalArgumentException();
		}
		setLabel("Delete <connector>"+child.getName());
		this.child = child;
		this.parent = child.getOwner();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	/**
	 * 
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void execute() {
		Debug.debug(this, "DeleteConnectorFromModel.execute -> "+child, 21);
		Assert.isNotNull(parent, "DeleteConnector.child has no parent");
		if(parent instanceof StructuredClassifier) {
			// save member ends
			child.eAdapters().clear();
			((StructuredClassifier)parent).getOwnedConnectors().remove(child);
		} else {
			throw new RuntimeException("Association.delete is not in a package");
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	/**
	 * 
	 */
	@Override
	public void redo() {
		execute();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	/**
	 * 
	 */
	@Override
	public void undo() {
		if(parent instanceof StructuredClassifier) {
			((StructuredClassifier)parent).getOwnedConnectors().add(child);
		}
	}
}
